const { PlaneModel } = require('../model/planeModel');
const { EnemyModel } = require('../model/enemyModel');
const g = require('../utils/global');
const { randXinSky } = require('../utils/helper');


cc.Class({
    extends: cc.Component,

    properties: {
        // 空战区域
        sky: {
            type: cc.Node,
            default: null,
        },
        // 血条
        blood: {
            type: cc.Sprite,
            default: null,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.enemyNumber = 0;
        this.enemyModels = {};
        // 开启碰撞
        this.collisionMgr = cc.director.getCollisionManager();
        this.collisionMgr.enabled = true;
        // 开启调试
        // this.collisionMgr.enabledDebugDraw = true;
        this.effectQueues = [];
        this.init();
    },

    // 开始战斗
    init() {
        this.planeModel = new PlaneModel();
        const sky = this.sky.getComponent('skyView');
        sky.setController(this);
        sky.initPlaneModel(this.planeModel);
        // 移动背景图
        sky.playMissionBackground();
        this.planeModel.moveBy(cc.v2(0, g.SKY_HEIGHT/4), 0);
        this.skyView = sky;
        // 是否撞机
        this.isCollision = false;
        this.collisionEnemy = null;
    },

    cleanCmd() {
        this.effectQueues = [];
        this.planeModel.cmd = [];
        const keys = Object.keys(this.enemyModels);
        for(const k of keys) {
            if (this.enemyModels[k]) {
                this.enemyModels[k].cmd = [];
            }
        }
    },

    // 战斗
    fighting(movePos) {
        cc.log(`start fight, move event ${movePos}`);
    },

    // 移动
    moving(touchLoc) {
        const deltaX = touchLoc.getDeltaX();
        const deltaY = touchLoc.getDeltaY();
        this.planeModel.delayMove(deltaX, deltaY);
    },

    // 新的敌机
    genNewEnemy() {
        this.enemyNumber++;
        const enemy = new EnemyModel(this.enemyNumber, randXinSky());
        this.enemyModels[this.enemyNumber] = enemy;
        return enemy;
    },

    // 敌机被英雄撞机
    collisionEnemyWithPlane(enemyModel, x, y, tag) {
        if (!this.enemyModels[enemyModel.id] || tag === g.CollisionTag.planeWing && this.planeModel.attackTimes !== 3){
            // 无效撞击
            return;
        }
        enemyModel.toDie(0);
        this.playBomb(0, x, y);
        delete this.enemyModels[enemyModel.id];
        // 防冲突
        if(this.isCollision) {
            this.isCollision = false;
            this.skyView.updateCollision([enemyModel, this.planeModel], this.effectQueues);
        } else {
            this.isCollision = true;
            this.collisionEnemy = enemyModel;
        }
    },

    // 英雄被撞机
    collisionPlaneWithEnemy(x, y) {
        this.planeModel.attackTimes--;
        if (this.planeModel.attackTimes <= 0) {
            // 去死吧
            this.planeModel.toDie(0);
            this.playBomb(g.AnimTime.planeBomb, x, y);
        } else {
            cc.log('plane under attacked');
            this.planeModel.attacked();
            this.playBomb(g.AnimTime.planeBomb, x, y);
        }
        // 防冲突
        if(this.isCollision) {
            this.isCollision = false;
            this.skyView.updateCollision([this.collisionEnemy, this.planeModel], this.effectQueues);
        } else {
            this.isCollision = true;
        }
    },
    // 被击中了
    enemyAttacked(enemyModel, x, y) {
        cc.log('enemy attacked');
        enemyModel.toDie(0);
        delete this.enemyModels[enemyModel.id];
        this.playBomb(0, x, y);
        this.skyView.updateCollision([enemyModel], this.effectQueues);
    },

    // 子弹消失
    fireBullet(bulletView) {
        cc.log('bullet fired');
        bulletView.node.destroy();
    },

    // 播放动效
    playBomb(playTime, x, y ) {
        this.effectQueues.push({
            action: g.EffectType.bomb,
            playTime,
            x,
            y,
        });
    },

});